package com.xr.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.xr.constant.MessageConstant;
import com.xr.exception.CustomException;
import com.xr.mapper.OrderSettingMapper;
import com.xr.pojo.OrderSetting;
import com.xr.pojo.OrderSettingExample;
import com.xr.service.OrderSettingService;
import com.xr.service.SetMealService;
import org.springframework.beans.factory.annotation.Autowired;

import javax.naming.ldap.Rdn;
import java.util.*;

import static com.xr.constant.MessageConstant.ORDER_NOTHANNUMBER;

@Service(interfaceClass = OrderSettingService.class)
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingMapper orderSettingMapper;

    //    批量添加
    @Override
    public void addBach(List<OrderSetting> list) {
        ArrayList<Object> objects = new ArrayList<>();
        objects.toArray();

        System.out.println("joinList" + list);
        if (list != null && list.size() > 0) {
            for (OrderSetting orderSetting : list) {
                //检查此数据是否存在
                OrderSetting orderDate = findCountByOrderDate(orderSetting.getOrderdate());
                System.out.println("countValue" + orderDate);
                if (orderDate !=null) { //存在做修改
                    orderSettingMapper.editNumberByOrderDate(orderSetting);
                } else {
                    //不存在做添加
                    orderSettingMapper.insertSelective(orderSetting);
                }

            }
        }
    }

    @Override
    public OrderSetting findCountByOrderDate(Date orderDate) {
        return orderSettingMapper.findCountByOrderDate(orderDate);
    }

    @Override
    public void editNumberByOrderDate(OrderSetting orderSetting) {
        orderSettingMapper.insertSelective(orderSetting);
    }

    //    根据月份获取预约时间
    @Override
    public List<Map> getOrderSettingByMonth(String date) {
        String begin = date + "-1";
        String end = date + "-31";
        Map<String, String> map = new HashMap<>();
        map.put("begin", begin);
        map.put("end", end);
//        根据日期查询预约范围
        List<OrderSetting> list = orderSettingMapper.getOrderSettingByMonth(map);
        List<Map> result = new ArrayList<>();
        if (list != null && list.size() > 0) {
            for (OrderSetting orderSetting : list) {
                Map<String, Object> m = new HashMap<>();
                m.put("date", orderSetting.getOrderdate().getDate());//date
                m.put("number", orderSetting.getNumber());
                m.put("reservations", orderSetting.getReservations());
                result.add(m);
            }
        }
        return result;
    }

    //    根据日期设置可预约人数
    @Override
    public void updateNumberByDate(OrderSetting orderSetting) throws CustomException {
        Date orderDate = orderSetting.getOrderdate();
        Integer number = orderSetting.getNumber();
        //根据日期查询会否进行了预约设置
        OrderSetting count = orderSettingMapper.findCountByOrderDate(orderDate);
        if (null != count) { //当前日期已进行过预约设置，需要更新
                if(count.getReservations()>number){
                        throw new CustomException(count.getOrderdate()+"中已预约数量不能大于可预约数量");
                }
                    orderSettingMapper.editNumberByOrderDate(orderSetting);
        } else {  //当前日期未进行预约设置 需要添加
            orderSettingMapper.insertSelective(orderSetting);
        }
    }
}
